using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 屏幕后处理基类
/// </summary>
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class PostEffectBase : MonoBehaviour
{
    /// <summary>
    /// 屏幕后处理使用的Shader
    /// </summary>
    public Shader shader;

    private Material _material;

    protected Material MyMaterial
    {
        get
        {
            if (shader == null || !shader.isSupported)
                return null;
            if (_material != null && _material.shader == shader)
                return _material;
            _material = new Material(shader);
            //不希望材质球被保存
            _material.hideFlags = HideFlags.DontSave;
            return _material;
        }
        set => _material = value;
    }

    protected virtual void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        SetProperties();
        if (MyMaterial != null)
            Graphics.Blit(source, destination, MyMaterial);
        else
            Graphics.Blit(source, destination);
    }

    protected virtual void SetProperties()
    {
        
    }
}